package cn.jiangjiesheng.bootstrap.fileport.domain.doc.generator;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import cn.jiangjiesheng.bootstrap.fileport.domain.doc.model.ReportDataModel;
import lombok.extern.slf4j.Slf4j;

import java.io.*;

/**
 * 生成word文件
 * https://poi.apache.org/components/document/
 * http://deepoove.com/poi-tl/
 * <p>
 * Created by dev@jiangjiesheng.cn on 2021/3/16  <br/>
 */
@Slf4j
public class TplWord {

    private final ByteArrayOutputStream out = new ByteArrayOutputStream();

    /**
     * 创建报告
     *
     * @param templ
     * @param inputData
     * @return
     */
    public TplWord(InputStream templ, ReportDataModel inputData)  {
        XWPFTemplate template = XWPFTemplate.compile(templ).render(inputData);
        try {
            template.writeAndClose(out);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public TplWord(InputStream templ, Configure config, ReportDataModel inputData)  {
        XWPFTemplate template = XWPFTemplate.compile(templ, config).render(inputData);
        try {
            template.writeAndClose(out);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public byte[] toBytes(){
        return out.toByteArray();
    }

    public OutputStream toOutputStream(){
        return out;
    }

    public InputStream toInputStream(){
        return new ByteArrayInputStream(toBytes());
    }
}
